package com.the8thfold.anton.ant

import grails.plugins.springsecurity.Secured
import org.apache.tools.ant.Project
import com.the8thfold.anton.users.User
import com.the8thfold.anton.users.Roles
import org.codehaus.groovy.grails.plugins.springsecurity.SpringSecurityUtils

class ResultController {

    def springSecurityService
    def scriptRunnerService

    @Secured(['ROLE_RESULTS_VIEW'])
    def index = {
        redirect(action: "list", params: params)
    }

    @Secured(['ROLE_RESULTS_VIEW'])
    def list = {
        if (SpringSecurityUtils.ifAllGranted(Roles.ROLE_ADMIN.toString())) {
            if (params.jobId) {
                [results: AntJobRunResults.findAllByJob(AntJob.findByJobId(params.jobId))]
            }
            else {
                [results: AntJobRunResults.list()]
            }
        } else {
            def results = AntJobRunResults.findAllVisibleByUserAndJob(springSecurityService.principal.userId, params.jobId)
            [results: results]
        }
    }

    @Secured(['ROLE_RESULTS_VIEW'])
    def view = {
        if (params.id){
            AntJobRunResults result = AntJobRunResults.findByRunResultsId(params.id)
            if (SpringSecurityUtils.ifAllGranted("ROLE_ADMIN") || AntJobUser.findByUserAndJob(User.findByUserId(springSecurityService.principal.userId),result.job)){
                [result : result]
            } else {
                redirect(controller: "login", action: "denied")
            }
        } else {
            redirect(controller: "login", action: "denied")
        }
    }

    @Secured(['ROLE_RESULTS_VIEW'])
    def viewLog = {
        def content = ""
        if (params.id) {
            AntJobRunResults result = AntJobRunResults.findByRunResultsId(params.id)
            if (result.runLog) {
                File logFile = new File(result.runLog)
                if (logFile.exists()){
                    content = logFile.text
                }
            }

            render(template: "viewLog", model: [content: content])
        }else {
            redirect(action: 'list', params: params)
        }
    }

    @Secured(['ROLE_RESULTS_VIEW'])
    def viewErrorLog = {
        def content = ""
        if (params.id) {
            AntJobRunResults result = AntJobRunResults.findByRunResultsId(params.id)
            if (result.runErrorLog) {
                File logFile = new File(result.runErrorLog)
                if (logFile.exists()){
                    content = logFile.text
                }
            }

            render(template: "viewErrorLog", model: [content: content])
        }else {
            redirect(action: 'list', params: params)
        }
    }

    @Secured(['ROLE_JOB_RUN'])
    def run = {
        if (params.id) {
            AntJobRunResults result = AntJobRunResults.findByRunResultsId(params.id)
            result.status = AntJobStatus.RUNNING
            result.runBy = User.findByUserId(springSecurityService.principal.userId)
            result.dateRun = new Date()
            scriptRunnerService.runJob(result)
            redirect(action: 'view', id: params.id, params:params)
        }else {
            redirect(action: 'list', params: params)
        }

    }

    @Secured(['ROLE_JOB_RUN'])
    def stop = {
        if (params.id) {
            AntJobRunResults result = AntJobRunResults.findByRunResultsId(params.id)
            Project antProject = scriptRunnerService.getRunningAntProject(result.runResultsId)
            if (!antProject){
                result.status = AntJobStatus.COMPLETE
                scriptRunnerService.runJob(result)
            }
            redirect(action: 'view', id: params.id, params:params)
        }else {
            redirect(action: 'list', params: params)
        }
    }

    @Secured(['ROLE_RESULTS_ADMIN'])
    def delete = {
        if (params.id){
            AntJobRunResults result = AntJobRunResults.findByRunResultsId(params.id)
            result.delete(flush: true)
        }
        redirect(action: 'list', params: params)
    }

    @Secured(['ROLE_RESULTS_ADMIN'])
    def updateField = {
        if (params.id){
            AntJobRunResults result = AntJobRunResults.findByRunResultsId(params.id)
            result.properties.put(params.field, params.value)
            result.save(flush: true, failOnError: true)
        }
        render("")
    }
}
